﻿namespace PrismEx.Events
{
    using System;

    using Microsoft.Practices.Prism.Events;

    public sealed class ReactiveSubscriptionToken : SubscriptionToken
    {
        private readonly IDisposable token;

        private bool disposed = false;

        public ReactiveSubscriptionToken(IDisposable token) : base(t => t.Dispose())
        {
            this.token = token;
        }

        ~ReactiveSubscriptionToken()
        {
            this.Dispose(false);
        }

        public override void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool explicitDispose)
        {
            if (!this.disposed)
            {
                if (explicitDispose)
                {
                    this.token.Dispose(); 
                }
            }

            this.disposed = true;
        }
    }
}